<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Users</title>
  <!-- 新 Bootstrap4 核心 CSS 文件 -->
  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  <!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单，包含了 popper.min.js -->
  <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
  <!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
  <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
  <script>
    function isNull(str) {
      if (str == "")
        return true;
      let regu = "^[ ]+$";
      let re = new RegExp(regu);
      return re.test(str);
    }

    $(function () {
      $("#div4addUser").hide();
      $("#btn4showhide").click(function () {
        if ($("#div4addUser").is(":hidden"))
          $("#div4addUser").show();
        else
          $("#div4addUser").hide();
      });
      $("#add").click(function () {
        let nameVal = $("#name").val();
        let passwordVal =  $("#password").val();
        let roleVal = $("#role option:selected").text();
        if (isNull(nameVal) || isNull(passwordVal) || isNull(roleVal)) {
          confirm("用户名和密码不能为空！");
          return;
        }
        let userBean = {
          "name" : nameVal,
          "password" : passwordVal,
          "role" : roleVal
        };
        $.ajax({
          type : "POST",
          url : "/addUsers",
          data : userBean,
          async : true,
          success : function (result) {
            alert(result.msg);
            location.href = "users";
          }
        })
      })
    })
  </script>
  <div id="selectResult1" style="width: 50%;margin: 0 auto">
    <table class="table table-white table-hover">
      <thead>
      <tr>
        <th>用户ID</th>
        <th>用户名称</th>
        <th>用户密码</th>
        <th>用户权限</th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
      <tr th:each="user : ${userList}">
        <td th:text="${user.id}"></td>
        <td th:text="${user.name}"></td>
        <td th:text="${user.password}"></td>
        <td th:text="${user.role}"></td>
        <td>
          <a th:href="@{/user/{id}(id=${user.id})}">修改</a>
          <a th:href="@{/deleteUser/{id}(id=${user.id})}" id="delete">删除</a>
        </td>
      </tr>
      </tbody>
    </table>
  </div>
<body>
<div id="selectResult" class="mx-auto mt-0 w-50">
  <div class="text-center">
    <input id="btn4showhide" type="button" value="显示隐藏添加用户">
  </div>
</div>
<div id="div4addUser" class="mx-auto mt-0 w-50 border border-success">
  <div class="text-center">
    <label>添加用户</label>
  </div>
  <form name="addUser" action="/addUser" method="post" enctype="multipart/form-data">
    <div class="form-group">
      <label for="name">用户名:</label>
      <input type="text" class="form-control" id="name" name="name">
    </div>
    <div class="form-group">
      <label for="password">密码:</label>
      <input type="password" class="form-control" id="password" name="password">
    </div>
    <div class="form-group">
      <label for="role">角色:</label>
      <select class="form-control" id="role" name="role">
        <option value="admin">admin</option>
        <option value="user">user</option>
      </select>
    </div>
    <div class="text-center">
      <input id="add" type="submit" value="添加用户"/>
    </div>
  </form>

</div>
</body>
</html>